package top.wshape1.rpc.starter;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import top.wshape1.rpc.compress.Compressor;
import top.wshape1.rpc.compress.CompressorFactory;
import top.wshape1.rpc.consumer.tolerant.FaultTolerantStrategy;
import top.wshape1.rpc.consumer.tolerant.FaultTolerantStrategyFactory;
import top.wshape1.rpc.loadbalance.LoadBalance;
import top.wshape1.rpc.loadbalance.LoadBalanceFactory;
import top.wshape1.rpc.message.serializer.Serializer;
import top.wshape1.rpc.message.serializer.SerializerFactory;
import top.wshape1.rpc.registry.RpcRegistry;
import top.wshape1.rpc.registry.RpcRegistryFactory;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2024-02-21
 */

public class JJRpcBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof RpcRegistry) {
            RpcRegistryFactory.setRegistry((RpcRegistry) bean);
        } else if (bean instanceof LoadBalance) {
            LoadBalanceFactory.setLoadBalance((LoadBalance) bean);
        } else if (bean instanceof Serializer) {
            SerializerFactory.setSerializer((Serializer) bean);
        } else if (bean instanceof FaultTolerantStrategy) {
            FaultTolerantStrategyFactory.setStrategy((FaultTolerantStrategy) bean);
        } else if (bean instanceof Compressor) {
            CompressorFactory.setCompressor((Compressor) bean);
        }

        return bean;
    }
}
